package service

import (
	"MyBlogv2/common/HttpCode"
	"MyBlogv2/common/models"
	"MyBlogv2/common/response"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func GetTagList(c *gin.Context) {
	pageNum, _ := strconv.Atoi(c.DefaultQuery("pageNum", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
	var tagVoList []models.TagVo
	var total int64
	models.Db.Model(new(models.Tag)).Count(&total)
	models.Db.Model(new(models.Tag)).
		Select("id, tag_name, created_at, updated_at").
		Order("created_at desc").
		Offset((pageNum - 1) * pageSize).
		Limit(pageSize).
		Find(&tagVoList)
	response.ResponseOK(c, gin.H{
		"page":  tagVoList,
		"total": total,
	})
}

func AddTag4Article(c *gin.Context) {
	var tagDto models.TagDto
	if err := c.ShouldBindJSON(&tagDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
	}
	tagDto.Id = 0
	models.Db.Model(new(models.TagDto)).Create(&tagDto)
	response.ResponseOK(c, nil)
}

func GetArticleTagList(c *gin.Context) {
	pageNum, _ := strconv.Atoi(c.DefaultQuery("pageNum", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
	var tagVoList []models.TagVo
	var total int64
	models.Db.Model(new(models.Tag)).Count(&total)
	models.Db.Table("t_tag t").
		Select("t.id, t.tag_name, t.created_at, t.updated_at, COUNT(at.article_id) as article_count").
		Joins("left join t_article_tag at on t.id = at.tag_id").
		Group("t.id, t.tag_name, t.created_at, t.updated_at").
		Order("t.created_at desc").
		Offset((pageNum - 1) * pageSize).
		Limit(pageSize).
		Find(&tagVoList)
	response.ResponseOK(c, gin.H{
		"page":  tagVoList,
		"total": total,
	})
}

func SearchTag(c *gin.Context) {
	var searchTagDto models.SearchTagDto
	if err := c.ShouldBindJSON(&searchTagDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
	}
	var tagVoList []models.TagVo
	db := models.Db.Model(new(models.TagVo))
	if searchTagDto.TagName != "" {
		db.Where("tag_name like ?", "%"+searchTagDto.TagName+"%")
	}
	if !time.Time(searchTagDto.StartTime).IsZero() {
		db.Where("created_at >= ?", searchTagDto.StartTime)
	}
	if !time.Time(searchTagDto.EndTime).IsZero() {
		db.Where("created_at <= ?", searchTagDto.EndTime)
	}
	db.Find(&tagVoList)
	for i, v := range tagVoList {
		var articleCount int64
		models.Db.Model(new(models.ArticleTag)).Where("tag_id = ?", v.Id).Count(&articleCount)
		tagVoList[i].ArticleCount = int(articleCount)

	}
	response.ResponseOK(c, tagVoList)
}

func GetTagById(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var tagVo models.TagVo
	models.Db.Model(new(models.TagVo)).Where("id = ?", id).First(&tagVo)
	var articleCount int64
	models.Db.Model(new(models.ArticleTag)).Where("tag_id = ?", id).Count(&articleCount)
	tagVo.ArticleCount = int(articleCount)
	response.ResponseOK(c, tagVo)
}

func AddOrUpdateTag(c *gin.Context) {
	var tagDto models.TagDto
	if err := c.ShouldBindJSON(&tagDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
	}
	if tagDto.Id == 0 {
		models.Db.Model(new(models.TagDto)).Create(&tagDto)
	} else {
		models.Db.Model(new(models.TagDto)).Where("id = ?", tagDto.Id).Updates(&tagDto)
	}
	response.ResponseOK(c, nil)
}

func DeleteTagByIds(c *gin.Context) {
	var ids []int
	if err := c.ShouldBindJSON(&ids); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
	}
	var articleCount int64
	models.Db.Model(new(models.ArticleTag)).Where("tag_id in (?)", ids).Count(&articleCount)
	if articleCount > 0 {
		response.CommonResponse(c, HttpCode.FAILURE, nil, "存在文章引用该标签，无法删除")
		return
	}
	models.Db.Model(new(models.Tag)).Where("id in (?)", ids).Delete(new(models.Tag))
	response.ResponseOK(c, nil)
}
